/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.business.boundary;

import java.security.Principal;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import meteocal.business.control.ForecastChecker;
import meteocal.business.control.WeatherChecker;
import meteocal.business.entity.Event;
import meteocal.business.entity.Weather;
import meteocal.business.security.entity.User;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import meteocal.business.entity.BadWeather;

/**
 *
 * @author luana
 */
@Stateless
public class EventFacade extends AbstractFacade<Event> {
    
     
    @PersistenceContext(unitName = "meteocalPU")
    EntityManager em;
    
    private String user;
    private String owner;
    
    @Inject
    Principal principal;
    
    private WeatherChecker wc;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public void searchCreator(String user){
        Query q2 = em.createQuery("SELECT u FROM USERS u where u.username = :nome");
        q2.setParameter("nome", user);
        User u= (User) q2.getSingleResult();
        System.out.println("utente ="+u.getUsername());
    }
    
    public EventFacade() {
        super(Event.class);
        
        wc = new WeatherChecker();
    }
    
    @Override
    public void create(Event e){
        
        
        Weather forecast = wc.checkWeather(e.getPlace(), e.getStarting_date(), e.getStartingTime());
        em.merge(forecast);
        e.setExpectedWeather(forecast);
       
        e.setCreator(em.find(User.class, principal.getName()));
        em.flush();
        super.create(e);
        
        
    }
    
    private String getUsername(){
        return principal.getName();
    }

    public String getUser() {
        return principal.getName();
    }

    public void setUser(String user) {
        this.user = user;
    }
    
    
    
    public Event getEvent (){
        return em.find(Event.class, principal.getName());      
    }
    
    public void removeEvent(Integer IDevent){
    em.createNamedQuery(Event.DELETE_EVENT, Event.class).setParameter("idEvent", IDevent).executeUpdate();
    }

    public void updateEvent(Event event) {
        Weather forecast = wc.checkWeather(event.getPlace(), event.getStarting_date(), event.getStartingTime());
        em.merge(forecast);
        event.setExpectedWeather(forecast);

        em.merge(event);
    }
    
     public List<Event> searchEventDate(Date date){
        return em.createNamedQuery(Event.FIND_EVENT_BY_DATE, Event.class).setParameter("starting_date",date).getResultList();
    }
    
    public List<Event> searchEventOfUser (String username){
       
        
        Query q2 = em.createQuery("SELECT u FROM USERS u where u.username = :nome");
        q2.setParameter("nome", username);
        User u= (User) q2.getSingleResult();
        System.out.println("utente ="+u.getUsername());
        Query q = em.createQuery("SELECT e FROM Event e WHERE e.creator = :user");
        q.setParameter("user",u );
        List<Event> lista = q.getResultList();
        return lista;
    }
    
    public List<Event> searchInParticipants (String username){
        
        Query findU = em.createQuery("SELECT u FROM USERS u WHERE u.username = :username");
        findU.setParameter("username", username);
        User u = (User) findU.getSingleResult();
        
        Query findE = em.createQuery("SELECT e FROM Event e WHERE e.partecipatingUsers = :user");
        findE.setParameter("user", u);
        
        return findE.getResultList();
        
    }
    public Event eventDetails(Integer idEvent){
        try{
            return em.createNamedQuery(Event.FIND_BY_IDEVENT, Event.class).setParameter("idEvent",idEvent).getSingleResult();
        } catch (NoResultException e){
            return null;
        }
    }
    
    public void merge(Event eventoSelezionato){
        em.merge(eventoSelezionato);
    }
    public void flush(){
        em.flush();
    }
}
  
        
  


